package com.jzo2o.customer.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.common.model.PageResult;
import com.jzo2o.customer.mapper.ServeProviderMapper;
import com.jzo2o.customer.mapper.WorkerCertificationAuditMapper;
import com.jzo2o.customer.mapper.WorkerCertificationMapper;
import com.jzo2o.customer.model.domain.WorkerCertificationAudit;
import com.jzo2o.customer.model.dto.request.WorkerCertificationAuditPageQueryReqDTO;
import com.jzo2o.customer.model.dto.response.WorkerCertificationAuditResDTO;
import com.jzo2o.customer.service.IWorkerCertificationAuditService;
import com.jzo2o.customer.service.IWorkerCertificationService;
import com.jzo2o.mvc.utils.UserContext;
import com.jzo2o.mysql.utils.PageHelperUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * @author XingDong
 */
@Service
public class WorkerCertificationAuditServiceImpl extends ServiceImpl<WorkerCertificationAuditMapper, WorkerCertificationAudit> implements IWorkerCertificationAuditService {

    @Resource
    private WorkerCertificationMapper workerCertificationMapper;
    @Resource
    private IWorkerCertificationService workerCertificationService;
    @Resource
    private ServeProviderMapper serveProviderMapper;

    @Override
    public PageResult<WorkerCertificationAuditResDTO> selectPage(WorkerCertificationAuditPageQueryReqDTO dto) {
        return PageHelperUtils.selectPage(dto, () ->
                baseMapper.selectByPage(dto)
        );
    }

    @Override
    public void audit(Long id, WorkerCertificationAuditResDTO dto) {
        // 获取服务人员id
        WorkerCertificationAudit workerCertificationAudit = lambdaQuery()
                .eq(WorkerCertificationAudit::getId, id)
                .one();
        Long serveProviderId = workerCertificationAudit.getServeProviderId();

        // 如果状态是前端传回来的成功
        if (dto.getCertificationStatus() == 2) {
            lambdaUpdate()
                    .set(WorkerCertificationAudit::getCertificationStatus, dto.getCertificationStatus())
                    .set(WorkerCertificationAudit::getAuditStatus, 1)
                    .set(WorkerCertificationAudit::getAuditTime, LocalDateTime.now())
                    .set(WorkerCertificationAudit::getAuditorId, UserContext.currentUserId())
                    .set(WorkerCertificationAudit::getAuditorName, UserContext.currentUser().getName())
                    .eq(WorkerCertificationAudit::getId, id)
                    .update();
            workerCertificationMapper.setSuccess(serveProviderId);
            // 向serveProvider表更新姓名
            serveProviderMapper.updateName(serveProviderId, workerCertificationAudit.getName());
        } else {
            lambdaUpdate()
                    .set(WorkerCertificationAudit::getCertificationStatus, dto.getCertificationStatus())
                    .set(WorkerCertificationAudit::getRejectReason, dto.getRejectReason())
                    .set(WorkerCertificationAudit::getAuditStatus, 1)
                    .set(WorkerCertificationAudit::getAuditTime, LocalDateTime.now())
                    .set(WorkerCertificationAudit::getAuditorId, UserContext.currentUserId())
                    .set(WorkerCertificationAudit::getAuditorName, UserContext.currentUser().getName())
                    .eq(WorkerCertificationAudit::getId, id)
                    .update();
            workerCertificationMapper.setFalse(serveProviderId);
        }
    }
}
